<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Batch extends Common
{

	//列表
    public function index()
    {
        $title = $this->request->get('title');
        $jxs_title = $this->request->get('jxs_title');

        $this->assign(array('title'=>$title));
        $this->assign(array('jxs_title'=>$jxs_title));

        $where = [];
        $where['isdel'] = 0;
        if($title)
            $where['title'] = array('like','%'.$title.'%');
        if($jxs_title)
            $where['id'] = array('exp','in(select city from cg_jxs where title like "%'.$jxs_title.'%")');


        $count = Db::table('cg_market_city')->where($where)->count();
        $data = Db::table('cg_market_city')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_market_city')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['jxs'] = Db::table('cg_jxs')->where(array('city'=>$v['id']))->field('id,title')->select();
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
		 $this->assign('maxpage',$maxpage);
		 $this->assign('count',$count);
        //查询分区
        $fenqu = Db::table('cg_market_city')->field('id,title')->select();
        $this->assign('fenqu',$fenqu);
        return $this->fetch('index');
    }

    //    批量设置
    public function batchSet(){
        $id = $this->request->post('id');
        $cityid = $this->request->post('cityid');
        if(!$id||!$cityid){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['city'] = $cityid;
        $data['provice'] = Db::table('cg_market_city')->where('id',$cityid)->value('provice');
        $data['zoning'] = Db::table('cg_market_provice')->where('id',$data['provice'])->value('zoning');
        $bool = Db::table('cg_jxs')->where('id','in',$id)->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"设置成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"设置失败"
            ]);die;
        }
    }

    /**
     * 替换所属城市
     */
    public function tihuan(){

        if($_POST){
            $old = $this->request->post('old');
            $new1 = $this->request->post('new1');
            $bool = Db::table('cg_jxs')->where('city',$old)->update(array('city'=>$new1));
            if(false!==$bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"替换成功"
                ]);
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"替换失败"
                ]);
            }
            die;
        }

        $id = $this->request->param('id');
        $this->assign('id',$id);
        $citytitle = Db::table('cg_market_city')->where('id',$id)->value('title');
        $this->assign('citytitle',$citytitle);
        $jxs = Db::table('cg_jxs')->where('city',$id)->select();
        $this->assign('jxs',$jxs);
        return $this->fetch();
    }


    /**
     * 返回城市json数据
     */
    public function provicejson(){
        //查询所有省份
        $data = Db::table('cg_market_city')->field('title,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }


    public function updateview(){
        $id = $this->request->param('id');
        $this->assign('id',$id);
        $data = Db::table('cg_market_city')->where(array('id'=>$id))->find();

        //查询所有非所属经销商
        $feisuoshu = Db::table('cg_jxs')->where('city','<>',$id)->field('id,title')->select();

        //查询所有所属经销商
        $suoshu = Db::table('cg_jxs')->where('city',$id)->field('id,title')->select();
        $this->assign('feisuoshu',$feisuoshu);
        $this->assign('suoshu',$suoshu);

        return $this->fetch();
    }


    public function zonetree(){
        return $this->fetch();
    }
}
